#include <cstdio>
#include <cassert>

// Included files for OpenGL Rendering
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif

#include "mesh.h"
#include "edge.h"
#include "vertex.h"
#include "triangle.h"
#include "glCanvas.h"
#include<map>
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int Triangle::next_triangle_id = 0;

// =======================================================================
// MESH DESTRUCTOR 
// =======================================================================

Mesh::~Mesh() {
  // delete all the triangles
  std::vector<Triangle*> todo;
  for (triangleshashtype::iterator iter = triangles.begin();
       iter != triangles.end(); iter++) {
    Triangle *t = iter->second;
    todo.push_back(t);
  }
  int num_triangles = todo.size();
  for (int i = 0; i < num_triangles; i++) {
    removeTriangle(todo[i]);
  }
  // delete all the vertices
  int num_vertices = numVertices();
  for (int i = 0; i < num_vertices; i++) {
    delete vertices[i];
  }
}

// =======================================================================
// MODIFIERS:   ADD & REMOVE
// =======================================================================

Vertex* Mesh::addVertex(const Vec3f &position) {
  int index = numVertices();
  Vertex *v = new Vertex(index, position);
  vertices.push_back(v);
  if (numVertices() == 1)
    bbox = BoundingBox(position,position);
  else 
    bbox.Extend(position);
  return v;
}


Triangle* Mesh::addTriangle(Vertex *a, Vertex *b, Vertex *c) {
  // create the triangle
  Triangle *t = new Triangle();
  // create the edges
  Edge *ea = new Edge(a,b,t);
  Edge *eb = new Edge(b,c,t);
  Edge *ec = new Edge(c,a,t);
  // point the triangle to one of its edges
  t->setEdge(ea);
  // connect the edges to each other
  ea->setNext(eb);
  eb->setNext(ec);
  ec->setNext(ea);
  // verify these edges aren't already in the mesh 
  // (which would be a bug, or a non-manifold mesh)
  //assert (edges.find(std::make_pair(a,b)) == edges.end());
  //assert (edges.find(std::make_pair(b,c)) == edges.end());
  //assert (edges.find(std::make_pair(c,a)) == edges.end());
  // add the edges to the master list
  edges[std::make_pair(a,b)] = ea;
  edges[std::make_pair(b,c)] = eb;
  edges[std::make_pair(c,a)] = ec;
  // connect up with opposite edges (if they exist)
  edgeshashtype::iterator ea_op = edges.find(std::make_pair(b,a)); 
  edgeshashtype::iterator eb_op = edges.find(std::make_pair(c,b)); 
  edgeshashtype::iterator ec_op = edges.find(std::make_pair(a,c)); 
  if (ea_op != edges.end()) { ea_op->second->setOpposite(ea); }
  if (eb_op != edges.end()) { eb_op->second->setOpposite(eb); }
  if (ec_op != edges.end()) { ec_op->second->setOpposite(ec); }
  // add the triangle to the master list
  assert (triangles.find(t->getID()) == triangles.end());
  triangles[t->getID()] = t;
  
  return t;
}


void Mesh::removeTriangle(Triangle *t) {
  Edge *ea = t->getEdge();
  Edge *eb = ea->getNext();
  Edge *ec = eb->getNext();
  Vertex *a = ea->getStartVertex();
  Vertex *b = eb->getStartVertex();
  Vertex *c = ec->getStartVertex();
  // remove these elements from master lists
  edges.erase(std::make_pair(a,b)); 
  edges.erase(std::make_pair(b,c)); 
  edges.erase(std::make_pair(c,a)); 
  triangles.erase(t->getID());
  // clean up memory
  delete ea;
  delete eb;
  delete ec;
  delete t;
}


// =======================================================================
// Helper functions for accessing data in the hash table
// =======================================================================

Edge* Mesh::getMeshEdge(Vertex *a, Vertex *b) const {
  edgeshashtype::const_iterator iter = edges.find(std::make_pair(a,b));
  if (iter == edges.end()) return NULL;
  return iter->second;
}

Vertex* Mesh::getChildVertex(Vertex *p1, Vertex *p2) const {
  vphashtype::const_iterator iter = vertex_parents.find(std::make_pair(p1,p2)); 
  if (iter == vertex_parents.end()) return NULL;
  return iter->second; 
}

void Mesh::setParentsChild(Vertex *p1, Vertex *p2, Vertex *child) {
  assert (vertex_parents.find(std::make_pair(p1,p2)) == vertex_parents.end());
  vertex_parents[std::make_pair(p1,p2)] = child; 
}


// =======================================================================
// the load function parses very simple .obj files
// the basic format has been extended to allow the specification 
// of crease weights on the edges.
// =======================================================================

void Mesh::Load(const char *input_file) {
  
  FILE *objfile = fopen(input_file,"r");
  if (objfile == NULL) {
    printf ("ERROR! CANNOT OPEN '%s'\n",input_file);
    return;
  }

  char line[200];
  char token[100];
  char atoken[100];
  char btoken[100];
  char ctoken[100];
  float x,y,z;
  int a,b,c,d,e;
  
  int index = 0;
  int vert_count = 0;
  int vert_index = 1;
  
  while (fgets(line, 200, objfile)) {   
    int token_count = sscanf (line, "%s\n",token);
    if (token_count == -1) continue;
    a = b = c = d = e = -1;
    if (!strcmp(token,"usemtl") ||
	!strcmp(token,"g")) {
      vert_index = 1; 
      index++;
    } else if (!strcmp(token,"v")) {
      vert_count++;
      sscanf (line, "%s %f %f %f\n",token,&x,&y,&z);
      addVertex(Vec3f(x,y,z));
    } else if (!strcmp(token,"f")) {
      int num = sscanf (line, "%s %s %s %s\n",token,
			atoken,btoken,ctoken);
      sscanf (atoken,"%d",&a);
      sscanf (btoken,"%d",&b);
      sscanf (ctoken,"%d",&c);
      assert (num == 4);
      a -= vert_index;
      b -= vert_index;
      c -= vert_index;
      assert (a >= 0 && a < numVertices());
      assert (b >= 0 && b < numVertices());
      assert (c >= 0 && c < numVertices());
      addTriangle(getVertex(a),getVertex(b),getVertex(c)); 
    } else if (!strcmp(token,"e")) {
      int num = sscanf (line, "%s %s %s %s\n",token,atoken,btoken,ctoken);
      assert (num == 4);
      sscanf (atoken,"%d",&a);
      sscanf (btoken,"%d",&b);
      if (!strcmp(ctoken,"inf")) x = 1000000; // this is close to infinity...
      else sscanf (ctoken,"%f",&x);
      Vertex *va = getVertex(a);
      Vertex *vb = getVertex(b);
      Edge *ab = getMeshEdge(va,vb);
      Edge *ba = getMeshEdge(vb,va);
      assert (ab != NULL);
      assert (ba != NULL);
      ab->setCrease(x);
      ba->setCrease(x);
    } else if (!strcmp(token,"vt")) {
    } else if (!strcmp(token,"vn")) {
    } else if (token[0] == '#') {
    } else {
      printf ("LINE: '%s'",line);
    }
  }
}


// =======================================================================
// PAINT
// =======================================================================

Vec3f ComputeNormal(const Vec3f &p1, const Vec3f &p2, const Vec3f &p3) {
  Vec3f v12 = p2;
  v12 -= p1;
  Vec3f v23 = p3;
  v23 -= p2;
  Vec3f normal;
  Vec3f::Cross3(normal,v12,v23);
  normal.Normalize();
  return normal;
}

void InsertNormal(const Vec3f &p1, const Vec3f &p2, const Vec3f &p3) {
  Vec3f normal = ComputeNormal(p1,p2,p3);
  glNormal3f(normal.x(), normal.y(), normal.z());
}


void Mesh::Paint(ArgParser *args) {

  // scale it so it fits in the window
  Vec3f center; center=bbox.getCenter(); //bbox.getCenter(center);
  float s = 1/bbox.maxDim();
  glScalef(s,s,s);
  glTranslatef(-center.x(),-center.y(),-center.z());

  // this offset prevents "z-fighting" bewteen the edges and faces
  // the edges will always win.
  glEnable(GL_POLYGON_OFFSET_FILL);
  glPolygonOffset(1.1,4.0);

  glColor3f(1,1,1);
  // draw the triangles
  /*if (args->gouraud) 
  {
	glBegin (GL_TRIANGLES);
	for (triangleshashtype::iterator iter = triangles.begin(); iter != triangles.end(); iter++) 
	{	
		Triangle *t = iter->second;
		
		Edge *e = t->getEdge();
		Edge *temp=e;
		Vertex *v = e->getStartVertex();
		vector<Vec3f> triangle_normals;
		
		Edge *e_next = e->getNext();
		Edge *temp2=e_next;
		Vertex *v2 = e_next->getStartVertex();
		vector<Vec3f> triangle_normals2;
		
		Edge *e_next_next = e_next->getNext();
		Edge *temp3 = e_next_next;
		Vertex *v3 = e_next_next->getStartVertex();
		vector<Vec3f> triangle_normals3;
		
		Vec3f a = (*t)[0]->getPos();
		Vec3f b = (*t)[1]->getPos();
		Vec3f c = (*t)[2]->getPos();
		Vec3f norm = ComputeNormal(a,b,c);
		triangle_normals.push_back(norm);
		triangle_normals2.push_back(norm);
		triangle_normals3.push_back(norm);
		
		do //look for and store all the triangles around the first point, compute normal of the nearby triangle
		{
			Edge *tempx = ( (temp->getNext())->getOpposite() );
			temp=temp->getNext();
			Triangle *t1 = tempx->getTriangle();
			Vec3f a1 = (*t1)[0]->getPos();
			Vec3f b1 = (*t1)[1]->getPos();
			Vec3f c1 = (*t1)[2]->getPos();
			Vec3f norm1 = ComputeNormal(a1,b1,c1);
			triangle_normals.push_back(norm1);
		}
		while(temp!=e);
		do //look for and store all the triangles around the second point, compute normal of the nearby triangle
		{
			Edge *tempx = ( (temp2->getNext())->getOpposite() );
			temp2=temp2->getNext();
			Triangle *t1 = tempx->getTriangle();
			Vec3f a1 = (*t1)[0]->getPos();
			Vec3f b1 = (*t1)[1]->getPos();
			Vec3f c1 = (*t1)[2]->getPos();
			Vec3f norm1 = ComputeNormal(a1,b1,c1);
			triangle_normals2.push_back(norm1);
		}
		while(temp2!=e_next);
		do //look for and store all the triangles around the third point, compute normal of the nearby triangle
		{
			Edge *tempx = ( (temp3->getNext())->getOpposite() );
			temp3=temp3->getNext();
			Triangle *t1 = tempx->getTriangle();
			Vec3f a1 = (*t1)[0]->getPos();
			Vec3f b1 = (*t1)[1]->getPos();
			Vec3f c1 = (*t1)[2]->getPos();
			Vec3f norm1 = ComputeNormal(a1,b1,c1);
			triangle_normals3.push_back(norm1);
		}
		while(temp3!=e_next_next);
		
		Vec3f average,average2,average3;  //add up all the normals from surrounding triangles
		for(unsigned int i=0; i<triangle_normals.size(); i++)
			average+=triangle_normals[i];
		for(unsigned int i=0; i<triangle_normals2.size(); i++)
			average2+=triangle_normals2[i];
		for(unsigned int i=0; i<triangle_normals3.size(); i++)
			average3+=triangle_normals3[i];
			
		int ii=triangle_normals.size();
		int ii2=triangle_normals2.size();
		int ii3=triangle_normals3.size();
		
		average=Vec3f( (average.x()/ ii),(average.y()/ii),(average.z()/ii) ); //find the average normal
		average2=Vec3f( (average2.x()/ ii2),(average2.y()/ii2),(average2.z()/ii2) );
		average3=Vec3f( (average3.x()/ ii3),(average3.y()/ii3),(average3.z()/ii3) );
		
		v->normal=average; //set normals
		v2->normal=average2;
		v3->normal=average3;
		
	}
	
	for (triangleshashtype::iterator iter = triangles.begin(); iter != triangles.end(); iter++) //apply normals
	{
		Triangle *t = iter->second;
		Vertex *a = (*t)[0];
		Vertex *b = (*t)[1];
		Vertex *c = (*t)[2];
		
		glNormal3f(a->normal.x(),a->normal.y(),a->normal.z());
		glVertex3f(a->x(),a->y(),a->z());
		glNormal3f(b->normal.x(),b->normal.y(),b->normal.z());
		glVertex3f(b->x(),b->y(),b->z());
		glNormal3f(c->normal.x(),c->normal.y(),c->normal.z());
		glVertex3f(c->x(),c->y(),c->z());
		
	}
	
   glEnd();
	
  } */
  //else {
    glBegin (GL_TRIANGLES);
    for (triangleshashtype::iterator iter = triangles.begin();
	 iter != triangles.end(); iter++) {
      Triangle *t = iter->second;
      Vec3f a = (*t)[0]->getPos();
      Vec3f b = (*t)[1]->getPos();
      Vec3f c = (*t)[2]->getPos();
      InsertNormal(a,b,c); 
      glVertex3f(a.x(),a.y(),a.z());
      glVertex3f(b.x(),b.y(),b.z());
      glVertex3f(c.x(),c.y(),c.z());
    }
    glEnd();
  //}
  glDisable(GL_POLYGON_OFFSET_FILL); 


  // =================================
  if (args->wireframe) {
    glDisable(GL_LIGHTING);

    // draw all the interior, non-crease edges
    glLineWidth(1);
    glColor3f(0,0,0);
    glBegin (GL_LINES);
    for (edgeshashtype::iterator iter = edges.begin();
	 iter != edges.end(); iter++) {
      Edge *e = iter->second;
      if (e->getOpposite() == NULL || e->getCrease() > 0) continue;
      Vec3f a = e->getStartVertex()->getPos();
      Vec3f b = e->getEndVertex()->getPos();
      glVertex3f(a.x(),a.y(),a.z());
      glVertex3f(b.x(),b.y(),b.z());
    }
    glEnd();

    // draw all the interior, crease edges
    glLineWidth(3);
    glColor3f(1,1,0);
    glBegin (GL_LINES);
    for (edgeshashtype::iterator iter = edges.begin();
	 iter != edges.end(); iter++) {
      Edge *e = iter->second;
      if (e->getOpposite() == NULL || e->getCrease() == 0) continue;
      Vec3f a = e->getStartVertex()->getPos();
      Vec3f b = e->getEndVertex()->getPos();
      glVertex3f(a.x(),a.y(),a.z());
      glVertex3f(b.x(),b.y(),b.z());
    }
    glEnd();

    // draw all the boundary edges
    glLineWidth(3);
    glColor3f(1,0,0);
    glBegin (GL_LINES);
    for (edgeshashtype::iterator iter = edges.begin();
	 iter != edges.end(); iter++) {
      Edge *e = iter->second;
      if (e->getOpposite() != NULL) continue;
      assert (e->getCrease() == 0);
      Vec3f a = e->getStartVertex()->getPos();
      Vec3f b = e->getEndVertex()->getPos();
      glVertex3f(a.x(),a.y(),a.z());
      glVertex3f(b.x(),b.y(),b.z());
    }
    glEnd();

    glEnable(GL_LIGHTING);
  }

  HandleGLError(); 
}

struct vec3f_comp {
  bool operator() (const Vec3f& V, const Vec3f& V2) const
  {
	return( sqrt( (V2.x()*V2.x()) + (V2.y()*V2.y()) + (V2.z()*V2.z()) ) < sqrt( (V.x()*V.x()) + (V.y()*V.y()) + (V.z()*V.z()) ) ); 
	}
};


// =================================================================
// SUBDIVISION
// =================================================================

void Mesh::LoopSubdivision() 
{
  printf ("Subdivide the mesh!\n");
  
  map<Vec3f,int,vec3f_comp> vertex_loc; //store vertices based on their locations for easy access
  for(int i=0; i<numVertices(); i++)
  {
	Vertex *a=getVertex(i);
	Vec3f vec=a->getPos();
	vertex_loc[vec]=a->getIndex();
  }
  
  for (triangleshashtype::iterator iter = triangles.begin(); iter != triangles.end(); iter++)
  {
	Triangle *t = iter->second;
	if(t->just_made==0) //don't subdivide triangles that came from the subdivision of a triangle
	{
		Vec3f v1 = t->getEdge()->getStartVertex()->getPos();
		Vec3f v2 = t->getEdge()->getNext()->getStartVertex()->getPos();
		Vec3f v3 = t->getEdge()->getNext()->getNext()->getStartVertex()->getPos();
		
		map<Vec3f,int>::iterator it; //for each vertex, check to see if it's in the map before making a new one
		it=vertex_loc.find(v1);
		Vertex *a;
		if(it!=vertex_loc.end()) 
		{
			a=getVertex(it->second);
		}
		else
		{
			a = (*t)[0];
			vertex_loc[a->getPos()]=a->getIndex();
		}
		
		it=vertex_loc.find(v2);
		Vertex *b;
		if(it!=vertex_loc.end())
		{
			b=getVertex(it->second);
		}
		else
		{
			b = (*t)[1];
			vertex_loc[b->getPos()]=b->getIndex();
		}
		
		it=vertex_loc.find(v3);
		Vertex *c;
		if(it!=vertex_loc.end())
		{
			c=getVertex(it->second);
		}
		else
		{
			c = (*t)[2];
			vertex_loc[c->getPos()]=c->getIndex();
		}
		
			//find the midpoints
		Vec3f a_b = Vec3f( (a->getPos().x()+b->getPos().x())/2, (a->getPos().y()+b->getPos().y())/2, (a->getPos().z()+b->getPos().z())/2 );
		Vec3f b_c = Vec3f( (b->getPos().x()+c->getPos().x())/2, (b->getPos().y()+c->getPos().y())/2, (b->getPos().z()+c->getPos().z())/2 ); 
		Vec3f a_c = Vec3f( (a->getPos().x()+c->getPos().x())/2, (a->getPos().y()+c->getPos().y())/2, (a->getPos().z()+c->getPos().z())/2 ); 
		
		//between a and b
		Vertex *d;
		it=vertex_loc.find(a_b);
		if(it!=vertex_loc.end())
		{
			d=getVertex(it->second);
		}
		else
		{
			d=addVertex(a_b);
			vertex_loc[a_b]=d->getIndex();
		}
			
		//between b and c
		Vertex *e;
		it=vertex_loc.find(b_c);
		if(it!=vertex_loc.end())
		{
			e=getVertex(it->second);
		}
		else
		{
			e=addVertex(b_c);
			vertex_loc[b_c]=e->getIndex();
		}
			
		//between a and c
		Vertex *f;
		it=vertex_loc.find(a_c);
		if(it!=vertex_loc.end())
		{
			f=getVertex(it->second);
		}
		else
		{
			f=addVertex(a_c);
			vertex_loc[a_c]=f->getIndex();
		}
			
		Triangle *tt=addTriangle(a,d,f); //add the triangles
		tt->just_made=1;
		
		Triangle *tt2=addTriangle(b,e,d); 
		tt2->just_made=1;
		
		Triangle *tt3=addTriangle(e,c,f); 
		tt3->just_made=1;
		
		Triangle *tt4=addTriangle(d,e,f);
		tt4->just_made=1;
		
		//tt4->getEdge()->setOpposite(tt2->getEdge()->getNext());
		//tt4->getEdge()->getNext()->setOpposite(tt3->getEdge());
		
		removeTriangle(t);
		//t->just_made=-1;
		iter=triangles.begin();
			
		}	
	}
		
	for (triangleshashtype::iterator iter = triangles.begin(); iter != triangles.end(); iter++) //reset just_made variable to 0 for the next subdivision
	{
		Triangle *t = iter->second;
		t->just_made=0;
	}
	
	vertex_loc.clear(); //clear the map for the next subdivision
}


// =================================================================
// SIMPLIFICATION
// =================================================================

void Mesh::Simplification(int target_tri_count) 
{
  // clear out any previous relationships between vertices
  vertex_parents.clear();

  printf ("Simplify the mesh! %d -> %d\n", numTriangles(), target_tri_count);
  
  for(int i=0; i<numVertices(); i++) //first, find and store the adjacent vertices for each
  {
	if(getVertex(i)->erased==false)
	{
		getVertex(i)->neighbors.clear();
		for(int j=0; j<numVertices(); j++)
		{
			if(j!=i)
			{
				Vertex *a=getVertex(i);
				Vertex *b=getVertex(j);
				Edge *e=getMeshEdge(a,b);
				if(e != NULL) //points a and b for a line
				{
					a->neighbors.push_back(b->getIndex());	//b is adjacent to a, so add it to a's list of neighbors
				}
			}
		}
	}
  }
  map<float, pair<int,int> > legal_edges; //the map of legal edges and the ID nums of their two vertices, map sorts by least edge length to most
  for(int i=0; i<numVertices(); i++)       //find the legal edges by checking each pair of adjacent vertices' neighbors
  {
	if(getVertex(i)->erased==false)
	{
		Vertex *a=getVertex(i);
		for(unsigned int j=0; j<a->neighbors.size(); j++)
		{
			Vertex *b=getVertex(a->neighbors[j]);
			int counter=0;
			for(unsigned int k=0; k<b->neighbors.size(); k++)
			{
				for(unsigned int l=0; l<a->neighbors.size(); l++)
				{
					if(b->neighbors[k]==a->neighbors[l])
						counter++;
				}
			}
			if (counter<=2)
			{
				Edge *e=getMeshEdge(a,b);
				map<float,pair<int,int> >::iterator iter = legal_edges.find(e->Length());
				bool already_there=false;
				while( iter != legal_edges.end() )
				{
					if(iter->second.second==a->getIndex())
					{
						already_there=true;
						break;
					}		
					iter++;
				}
				if(already_there == false && e->Length() != 0) //this is a legal edge, add it's length and vertex ID numbers to the map
					legal_edges[e->Length()]=make_pair(a->getIndex(),b->getIndex());
			}	
		}
	}
  }

  map<float, pair<int,int> >::iterator iter;
  int counter=(int)(numTriangles()*.10);
  for(iter=legal_edges.begin(); iter!=legal_edges.end() && counter>=0; iter++) //go through the list of valid edges, starting with the shortest
  {
	num_d+=1;
	Vertex *a=getVertex(iter->second.first);
	Vertex *b=getVertex(iter->second.second);
	//Edge *e=getMeshEdge(a,b);

	Vec3f pos=a->getPos();
	Vec3f mid = Vec3f( (a->getPos().x()+b->getPos().x())/2, (a->getPos().y()+b->getPos().y())/2, (a->getPos().z()+b->getPos().z())/2 );
	for(int i=0; i<numVertices(); i++) //adjust accordingly
	{
		Vertex *aa=getVertex(i);
		Vec3f aa_pos=aa->getPos();
		if(aa->getIndex() != a->getIndex() && aa_pos == pos)
		{
			aa->setPos(mid);
		}
	}
	b->setPos(mid);
	a->setPos(b->getPos());
	a->erased=true;
	counter--;
  }
  
}

 